package xml改纯注解.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(public void xml改纯注解.service..*(..))")
    public void pt(){};
    @Before("pt()")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("[debug]前置通知，方法名："+methodName+",参数： "+args);
    }
    @After("pt()")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("[debug]后置通知，方法名："+methodName);

    }



    @AfterReturning(value = "pt()", returning = "result")
    public void afterReturningMethod(JoinPoint joinPoint, Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("[debug]返回通知，方法名："+methodName+"，结果："+result);
    }

    @AfterThrowing(value = "pt()", throwing = "ex")
    public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("[debug]异常通知，方法名："+methodName+"，异常："+ex);
    }

    @Around("pt()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        Object result = null;
        try {
            System.out.println("[debug]目标对象方法名："+methodName);
            System.out.println("[debug]目标对象方法参数："+args);
            System.out.println("[debug]环绕通知-->目标对象方法执行之前");
            //目标对象（连接点）方法的执行
            result = joinPoint.proceed();
            System.out.println("[debug]环绕通知-->目标对象方法返回值之后");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("[debug]环绕通知-->目标对象方法出现异常时");
        } finally {
            System.out.println("[debug]环绕通知-->目标对象方法执行完毕");
        }
        return result;
    }
}
